<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="厂站名" prop="factoryStationName">
        <el-input
          v-model="queryParams.factoryStationName"
          placeholder="请输入厂站名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="factoryStationType">
        <el-select v-model="queryParams.factoryStationType" placeholder="类型" clearable size="small"
                   :style="{width: '100%'}">
          <el-option v-for="dict in dict.type.factory_station_type"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:role:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="false"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getPage"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="page" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="厂站id" prop="id" width="100"/>
      <el-table-column label="厂站名" prop="factoryStationName" width="150"/>
      <el-table-column label="类型" prop="factoryStationType" width="120">
        <template slot-scope="scope">
          <div v-for="dict in dict.type.factory_station_type">
            <span v-if="dict.value === scope.row.factoryStationType">{{dict.label}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="正常水位" prop="normalWaterlevel" align="center" width="120"/>
      <el-table-column label="正常库容" prop="ttcp" align="center" width="120"/>
      <el-table-column label="死水位" prop="ddz" align="center" width="120"/>
      <el-table-column label="死库容" prop="ddcp" align="center" width="120"/>
      <el-table-column label="坝下水位" prop="ppdwz" align="center" width="120"/>
      <el-table-column label="装机容量" prop="insCap" align="center" width="120"/>
      <el-table-column label="尾水位" prop="tailWaterlevel" align="center" width="120"/>
      <el-table-column label="出力系数" prop="clxs" align="center" width="120"/>
      <el-table-column label="水头损失系数" prop="stssxs" align="center" width="120"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getPage"
    />

    <!-- 添加或修改解析元素对话框 -->
    <el-dialog :visible.sync="open" :title="title" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="medium" label-width="150px">
        <el-col :span="24">
          <el-form-item label="厂站id" prop="id">
            <el-input placeholder="请输入厂站id" v-model="form.id" :readonly="dialogType === 'update'"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="厂站名称" prop="factoryStationName">
            <el-input placeholder="请输入厂站名称" v-model="form.factoryStationName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="流域编码" prop="basCd">
            <el-input placeholder="请输入流域编码" v-model="form.basCd"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="组织编码" prop="orgCd">
            <el-input placeholder="请输入组织编码" v-model="form.orgCd"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="类型" prop="factoryStationType">
            <el-select v-model="form.factoryStationType" placeholder="类型" clearable size="small"
                       :style="{width: '100%'}">
              <el-option v-for="dict in dict.type.factory_station_type"
                         :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="流域面积" prop="basArea">
            <el-input placeholder="请输入流域面积" v-model="form.basArea"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="汛限水位" prop="fsltdz">
            <el-input placeholder="请输入汛限水位" v-model="form.fsltdz"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="正常水位" prop="normalWaterlevel">
            <el-input placeholder="请输入正常水位" v-model="form.normalWaterlevel"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="正常库容" prop="ttcp">
            <el-input placeholder="请输入库容(106m3)" v-model="form.ttcp"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="坝下水位" prop="ppdwz">
            <el-input placeholder="请输入坝下水位" v-model="form.ppdwz"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="设计水位" prop="dsflz">
            <el-input placeholder="请输入设计水位" v-model="form.dsflz"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="校核水位" prop="ckflz">
            <el-input placeholder="请输入校核水位" v-model="form.ckflz"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="死水位" prop="ddz">
            <el-input placeholder="请输入死水位" v-model="form.ddz"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="死库容" prop="ddcp">
            <el-input placeholder="请输入死库容" v-model="form.ddcp"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="装机容量" prop="insCap">
            <el-input placeholder="请输入装机容量" v-model="form.insCap"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="尾水位" prop="tailWaterlevel">
            <el-input placeholder="请输入尾水位" v-model="form.tailWaterlevel"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="出力系数" prop="clxs">
            <el-input placeholder="请输入出力系数" v-model="form.clxs"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="水头损失系数" prop="stssxs">
            <el-input placeholder="请输入水头损失系数" v-model="form.stssxs"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    page,
    getAllFactoryStation,
    getFactoryStationDetail,
    insertFactoryStation,
    updateFactoryStation,
    delFactoryStation,
  } from "@/api/commonconfig/factorystation";

  export default {
    dicts: ['factory_station_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        page: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {
          id: '',
          factoryStationName: '',
          factoryStationType: '',
          basArea: '',
          fsltdz: '',
          normalWaterlevel: '',
          ttcp: '',
          dsflz: '',
          ckflz: '',
          ddz: '',
          ddcp: '',
          tailWaterlevel: '',
          clxs: '',
          stssxs: '',
        },
        dialogType: '',
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          factoryStationName: '',
          factoryStationType: '',
        },
        // 表单校验
        rules: {
          id: [{
            required: true,
            message: '请输入厂站id',
            trigger: 'blur'
          }],
          factoryStationName: [{
            required: true,
            message: '请输入厂站名称',
            trigger: 'blur'
          }],
          factoryStationType: [{
            required: true,
            message: '请选择类型',
            trigger: 'blur'
          }],
          basArea: [{
            required: true,
            message: '请输入流域面积',
            trigger: 'blur'
          }],
          fsltdz: [{
            required: true,
            message: '请输入汛限水位',
            trigger: 'blur'
          }],
          normalWaterlevel: [{
            required: true,
            message: '请输入正常水位',
            trigger: 'blur'
          }],
          ttcp: [{
            required: true,
            message: '请输入正常库容',
            trigger: 'blur'
          }],
          ppdwz: [{
            required: true,
            message: '请输入坝下水位',
            trigger: 'blur'
          }],
          dsflz: [{
            required: true,
            message: '请输入设计水位',
            trigger: 'blur'
          }],
          ckflz: [{
            required: true,
            message: '请输入校核水位',
            trigger: 'blur'
          }],
          ddz: [{
            required: true,
            message: '请输入死水位',
            trigger: 'blur'
          }],
          ddcp: [{
            required: true,
            message: '请输入死库容',
            trigger: 'blur'
          }],
          insCap: [{
            required: true,
            message: '请输入装机容量',
            trigger: 'blur'
          }],
          tailWaterlevel: [{
            required: true,
            message: '请输入尾水位',
            trigger: 'blur'
          }],
          clxs: [{
            required: true,
            message: '请输入出力系数',
            trigger: 'blur'
          }],
          stssxs: [{
            required: true,
            message: '请输入水头损失系数',
            trigger: 'blur'
          }],
        },
      };
    },
    created() {
      this.getPage();
    },
    methods: {
      /** 查询分页列表 */
      getPage() {
        this.loading = true;
        page(this.queryParams).then(response => {
            this.page = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: '',
          factoryStationName: '',
          factoryStationType: '',
          basArea: '',
          fsltdz: '',
          normalWaterlevel: '',
          ttcp: '',
          dsflz: '',
          ckflz: '',
          ddz: '',
          ddcp: '',
          tailWaterlevel: '',
          clxs: '',
          stssxs: '',
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getPage();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.title = "新增厂站信息";
        this.open = true;
        this.dialogType = "add";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getFactoryStationDetail(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改厂站信息";
          this.dialogType = "update";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        let that = this;
        that.$refs["form"].validate(valid => {
          if (valid) {
            if ('update' === that.dialogType) {
              updateFactoryStation(that.form).then(response => {
                that.$modal.msgSuccess("修改成功");
                that.open = false;
                that.getPage();
              });
            } else {
              insertFactoryStation(that.form).then(response => {
                that.$modal.msgSuccess("新增成功");
                that.open = false;
                that.getPage();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const id = row.id || this.ids;
        this.$modal.confirm('是否确认删除？').then(function () {
          return delFactoryStation(id);
        }).then(() => {
          this.getPage();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/user/export', {
          ...this.queryParams
        }, `user_${new Date().getTime()}.xlsx`)
      },
    }
  };
</script>
